package com.iprojectmanager.views;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.iprojectmanager.entities.*;
import com.iprojectmanager.managers.*;

/**
 * @author Jons
 */
public class VistaOperacionRol extends Vista
{
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
    {
        if(request.getParameter("operacionesroles") != null)
        {
            if(request.getParameter("operacionesroles").compareTo("Crear") == 0)
            {
                mainView(response.getWriter(),request);
            }else
            {
                chooseRol(response.getWriter(),request.getParameter("operacionesroles"),request);
            }
        }
        else if(request.getParameter("rol") != null)
        {
            modifyView(response.getWriter(),request.getParameter("rol"),request);
        }
    }


    //para crear roles
    public void mainView(PrintWriter out,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        
        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Creacion De Roles",root));

        String form =
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getCrearRol() + "</div>" +
                        Utilities.getFooter() +
                "</div>";

        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    public void modifyView(PrintWriter out,String id,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");

        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Modificacion De Roles",root));

        String form =
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getModificarRol(id) + "</div>" +
                        Utilities.getFooter() +
                "</div>";

        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    public void chooseRol(PrintWriter out,String op,HttpServletRequest request)
    {
        HttpSession user = request.getSession();
        Administrador root = (Administrador)user.getAttribute("user");
        out.println(Utilities.getDocType());
        out.println("<HTML>" + Utilities.getHead("Administrador del Sistema"));
        out.println("<div id=\"container\">");
        out.println(Utilities.getIntro("Eleccion De Rol",root));

        String form =
        	"<div id=\"pageContent\">"+
                        "<div id=\"selection1\">" + this.getEscogerRol(op) + "</div>" +
                        Utilities.getFooter() +
                "</div>";

        out.println(form);
        out.println(Utilities.getMenu());
        out.println("</div> <!-- fin container---></BODY></HTML>");
        out.close();
    }

    @Override
    public String getServletInfo()
    {
        return "Vista de ABC de Roles";
    }

    public String getCrearRol()
    {
        return
                "<form action=\"RolControl\" method=\"GET\">" +
                    "<h3><span>Id. de rol</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbIdRol\" size=\"10\" readonly>" +
                    "</span></p>" +
                    "<h3><span>Nombre</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"text\" name=\"tbNombreRol\" size=\"30\">" +
                    "</span></p>" +
                    "<h3><span>Cambios</span></h3>" +
                    "<p class=\"p1\"><span>" +
                        "<input type=\"submit\" value=\"Guardar\" name=\"CA\">" +
                        "<input type=\"reset\" value=\"Reset\" name=\"CC\">" +
                    "</span></p>" +
                "<form>";
    }

    public String getModificarRol(String id)
    {
        RolManager manager = new RolManager();
        Rol rol = manager.getRoles(Integer.valueOf(id));

        return
            "<form action=\"RolControl\" method=\"GET\">" +
                "<h3><span>Id. de rol</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbIdRol\" size=\"10\" readonly value=\"" + rol.getId() + "\">" +
                "</span></p>" +
                "<h3><span>Nombre</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"text\" name=\"tbNombreRol\" size=\"30\" value=\"" + rol.getNombre() + "\">" +
                "</span></p>" +
                "<h3><span>Cambios</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<input type=\"submit\" value=\"Guardar\" name=\"MA\">" +
                    "<input type=\"reset\" value=\"Reset\" name=\"MC\">" +
                "</span></p>" +
            "<form>";
    }

    public String getEscogerRol(String op)
    {
        RolManager manager = new RolManager();
        ArrayList<Rol> roles = manager.getRoles();
        String table = "";

            for(Rol rol : roles)
            {
                table +=
                    "<tr>" +
                        "<td><input name=\"rol\" value=\""+ rol.getId() +"\" type=\"checkbox\" size=\"20\"></td>" +
                        "<td>"+ rol.getId() +"</td>"+
                        "<td>"+ rol.getNombre() + "</td>"+
                    "</tr>";
            }

        return
                "<h3><span>Seleccione Algun Rol</span></h3>" +
                "<p class=\"p1\"><span>" +
                    "<form action=\"RolControl\" method=\"GET\">" +
                        "<Table border=\"1\" cellspacing=\"1\">" +
                            "<tr>" +
                                "<b><th>Elegir</th><th>Id</th><th>Rol</th></b>" +
                            "</tr>" +
                                table +
                        "</Table>" +
                        "<br></br>" +
                        "<button type=\"submit\" name=\"operacion\">"+ op +"</button>" +
                     "</form>" +
                "</span></p>";
    }
}